<template>
	<view>
		<uni-forms :model="model" ref="uForm" :errorType="form.errorType">
			<uni-forms-item class="form-item" prop="phone" label="手机号" :label-position="form.labelPosition">
				<input v-model="model.phone" placeholder="请输入新的手机号" type="text" />
			</uni-forms-item>
		</uni-forms>
		<button class="button" type="primary" @click="submit">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				model:{phone:""},
				form: {
					errorType: ['message'],
					labelPosition: 'left'
				},
				rules: {
					phone: [{
							required: true,
							message: '请填写手机号码',
							trigger: ['change', 'blur']
						},
						{
							validator: (rule, value, callback) => {
								let iphoneReg = (
									/^(13[0-9]|14[1579]|15[0-3,5-9]|16[6]|17[0123456789]|18[0-9]|19[89])\d{8}$/
								); //手机号码
								if (!iphoneReg.test(value)) {
									callback(new Error('格式不正确'));
								}
								callback();
							},
							trigger: ['change', 'blur']
						}
					],
				},
			};
		},
		methods: {
			submit() {
				this.$refs.uForm.validate((valid) => {

					console.log(valid);
					if (valid) {
						uni.request({
							url: "http://localhost:8081/user/userinfo",
							method: "POST",
							data: {
                                uid:getApp().globalData.id,
								uphone:this.model.phone
							}
						}).then(res => {

							if (res[1].data.code==200) {
								uni.showToast({
									icon: 'success',
									title: "等待审核",
									duration:1000,
								});
									setTimeout(function() {
							         uni.navigateBack({})
									}, 1000);
							}
						})
					} else {
						uni.showToast({
							icon: 'error',
							title: "请填写完整"
						})
					}
				});
			}
		},
		mounted() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style>
	.button{
		width: 90%;
		background-color: #55aa7f;
	}
</style>
